#include <WinSock2.h>
#include "mysql.hpp"
#include <iostream>
#include <thread>
#include "connection_pool.hpp"
#include "SyllabusUtil.hpp"
#include "ormpp_cfg.hpp"
#include "utils_dao.hpp"
#include "cinatra.hpp"
#include "feather_cfg.hpp"
using namespace std::string_literals;
using namespace ormpp;
using namespace cinatra;
#include "SyllabusValidate.hpp"
#include "SyllabusController.hpp"
#include "api/api_admin.hpp"
#include "api/api_plan.hpp"
#include "api/api_base.hpp"
#include "api/api_course.hpp"
#include "api/api_user.hpp"
#include "api/other.hpp"
void init() {
    feather_cfg cfg{};
    bool ret = ormpp::config_manager::from_file(cfg, "./mysql.json");
    if (!ret) {
        printf("无mysql配置文件");
        exit(1);
    }
    dao_t<dbng<mysql>>::init(cfg.db_conn_num, cfg.db_ip.data(), cfg.user_name.data(), cfg.pwd.data(),cfg.db_name.data(), cfg.timeout);
}

int main()
{
    init();
    //controll
    http_server server(std::thread::hardware_concurrency());
    server.set_static_dir("");
    server.enable_http_cache(false);//set global cache
    server.set_res_cache_max_age(86400);
    bool r = server.listen("0.0.0.0", "8090");
    if (!r) {
        return -1;
    }
    try {

        server.set_http_handler<GET, POST>("/GetUserManager", &GetUserManager);
        server.set_http_handler<GET, POST>("/AddUserManager", &AddUserManager);
        server.set_http_handler<GET, POST>("/DeleteUserManager", &DeleteUserManager);
        server.set_http_handler<GET, POST>("/GetFlowNow", &GetFlowNow);
        server.set_http_handler<GET, POST>("/StartFlow", &StartFlow);
        server.set_http_handler<GET, POST>("/CancelFlow", &CancelFlow);
        server.set_http_handler<GET, POST>("/GetRequire_Syllabus", &GetRequire_Syllabus);
        server.set_http_handler<GET, POST>("/SaveRequire_Syllabus", &SaveRequire_Syllabus);
        server.set_http_handler<GET, POST>("/DeleteRequire_Syllabus", &DeleteRequire_Syllabus);
        server.set_http_handler<GET, POST>("/GetSyllabus", &GetSyllabus);
        server.set_http_handler<GET, POST>("/SaveSyllabus", &SaveSyllabus);
        server.set_http_handler<GET, POST>("/DeleteSyllabus", &DeleteSyllabus);
        server.set_http_handler<GET, POST>("/GetFinishRequire_Goal", &GetFinishRequire_Goal);
        server.set_http_handler<GET, POST>("/SaveFinishRequire_Goal", &SaveFinishRequire_Goal);
        server.set_http_handler<GET, POST>("/DeleteFinishRequire_Goal", &DeleteFinishRequire_Goal);
        server.set_http_handler<GET, POST>("/GetCourse", &GetCourse);
        server.set_http_handler<GET, POST>("/SaveCourse", &SaveCourse);
        server.set_http_handler<GET, POST>("/DeleteCourse", &DeleteCourse);
        server.set_http_handler<GET, POST>("/GetGoal", &GetGoal);
        server.set_http_handler<GET, POST>("/SaveGoal", &SaveGoal);
        server.set_http_handler<GET, POST>("/DeleteGoal", &DeleteGoal);
        server.set_http_handler<GET, POST>("/GetRequire", &GetRequire);
        server.set_http_handler<GET, POST>("/SaveRequire", &SaveRequire);
        server.set_http_handler<GET, POST>("/DeleteRequire", &DeleteRequire);
        server.set_http_handler<GET, POST>("/login_now", &login_now);
        server.set_http_handler<GET, POST>("/login", &login);
        server.set_http_handler<GET, POST>("/GetFinishRequire", &GetFinishRequire);
        server.set_http_handler<GET, POST>("/SaveFinishRequire", &SaveFinishRequire);
        server.set_http_handler<GET, POST>("/DeleteFinishRequire", &DeleteFinishRequire);
        server.set_http_handler<GET, POST>("/getSchool", &getSchool);
        server.set_http_handler<GET, POST>("/GetTeachGoalJudge", &GetTeachGoalJudge);
        server.set_http_handler<GET, POST>("/SaveTeachGoalJudge", &SaveTeachGoalJudge);
        server.set_http_handler<GET, POST>("/DeleteTeachGoalJudge", &DeleteTeachGoalJudge);
        server.set_http_handler<GET, POST>("/GetTeachGoal", &GetTeachGoal);
        server.set_http_handler<GET, POST>("/SaveTeachGoal", &SaveTeachGoal);
        server.set_http_handler<GET, POST>("/DeleteTeachGoal", &DeleteTeachGoal);
        server.set_http_handler<GET, POST>("/getSyllabusTeachGoal", &getSyllabusTeachGoal);
        server.set_http_handler<GET, POST>("/GetJudge", &GetJudge);
        server.set_http_handler<GET, POST>("/SaveJudge", &SaveJudge);
        server.set_http_handler<GET, POST>("/DeleteJudge", &DeleteJudge);
        server.set_http_handler<GET, POST>("/GetPlanData", &getPlanData);
        server.set_http_handler<GET, POST>("/SaveBaseplan", &SaveBaseplan);
        server.set_http_handler<GET, POST>("/DeletePlan", &DeletePlan);
        server.set_http_handler<GET, POST>("/GetAllGrades", &GetAllGrades);
        server.set_http_handler<GET, POST>("/GetProfession", &GetProfession);
        server.set_http_handler<GET, POST>("/SaveProfession", &SaveProfession);
        server.set_http_handler<GET, POST>("/DeleteProfession", &DeleteProfession);
        server.set_http_handler<GET, POST>("/GetFlow", &GetFlow);
        server.set_http_handler<GET, POST>("/SaveFlow", &SaveFlow);
        server.set_http_handler<GET, POST>("/DeleteFlow", &DeleteFlow);
        server.set_http_handler<GET, POST>("/GetAllUser", &GetAllUser, check_start_end_input{});
        server.run();
    } catch (const std::exception &ex) {
        printf("wrong,%s", ex.what());
    }
}
//TODO:用户修改自己信息界面
//TODO:每个表增加创建用户、更新用户、时间
//TODO:用户展示的是自己的指标、教学目标，如果能看到其他人的，要权限修改
//TODO:连接表要用连表查询，不能单用id
//TODO:分页